package com.mobilg.dating;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ProfileActivity extends Activity {
	private int myYear, myMonth, myDay;
	private String firstname, email, password, gender, searchedGender;
	private double latitude, longitude;
	static final int ID_DATEPICKER = 0;
	static final int GET_CURRENT_LOCATION = 1;
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {	
		super.onActivityResult(requestCode, resultCode, data);	
		
		 switch(requestCode) {
	      case GET_CURRENT_LOCATION: 
	            if (resultCode == RESULT_OK) {
	                latitude = data.getDoubleExtra("CURRENT_LOCATION_LATITUDE", 0); 
	                longitude = data.getDoubleExtra("CURRENT_LOCATION_LONGITUDE",0);	                
	                Toast.makeText(this, "Your current location: " + "lat:" + latitude+" long:" + longitude, Toast.LENGTH_LONG).show();
	                //startActivity(new Intent(Intent.ACTION_VIEW, "RESULT_OK"));
	                break;
	            }
	            else {
	            	latitude = data.getDoubleExtra("CURRENT_LOCATION_LATITUDE", 0);
	            	longitude = data.getDoubleExtra("CURRENT_LOCATION_LATITUDE", 0);
	                Toast.makeText(this, "Your current location: " + "lat:" + latitude+" long:"+longitude, Toast.LENGTH_LONG).show();	               	                
	                break;
	            }
	      }
	}


	private DbAdapter mDbHelper;
	
	EditText firstname_txt,email_txt,password_txt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile);
		
		try{
			
		Intent mainScreenIntent = getIntent();
		gender = mainScreenIntent.getStringExtra("PROFILE_GENDER");
		searchedGender = mainScreenIntent.getStringExtra("PREFERRED_GENDER");
	
		mDbHelper = new DbAdapter(this);
		mDbHelper.open();
        
        Button datePickerButton = (Button) findViewById(R.id.selectdate_btn);
        datePickerButton.setOnClickListener(datePickerButtonOnClickListener);
        
        Button submitButton = (Button) findViewById(R.id.search_button);
        submitButton.setOnClickListener(searchButtonOnClickListener);
        
        
        firstname_txt = (EditText) findViewById(R.id.firstname_txt);        
        email_txt = (EditText) findViewById(R.id.email_txt);
        password_txt = (EditText) findViewById(R.id.password_txt);        
        latitude = (float) 0.0;
        longitude = (float) 0.0;
        
        
       
        }catch(Exception ex)
        {
        	Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
        }
	}
	
	
	private Button.OnClickListener searchButtonOnClickListener
	= new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			
	        try{	        		
	        		email = email_txt.getText().toString();
	        		firstname = firstname_txt.getText().toString();
	        		password = password_txt.getText().toString();
	        			        		
					Cursor mCursor = mDbHelper.fetchProfile(email);
					if (mCursor.getCount()==0) {
						startActivityForResult(new Intent(ProfileActivity.this,LocationActivity.class), GET_CURRENT_LOCATION);
						long returnCode = mDbHelper.createProfile(String.valueOf(myYear), String.valueOf(myMonth), String.valueOf(myDay), firstname, email, password, latitude, longitude,gender,searchedGender);
						if(returnCode != -1){
							Toast.makeText(ProfileActivity.this, "You have successfully created your profile, lets search your matches:)", 
									Toast.LENGTH_LONG).show();
						}
					}
					else {
						Toast.makeText(ProfileActivity.this, "Already a member of the service, please check your email address", 
								Toast.LENGTH_LONG).show();
					}					
	        }catch(Exception ex){
	        	Toast.makeText(ProfileActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();	
	        }
		}

	};
	
	private Button.OnClickListener datePickerButtonOnClickListener
	= new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			final Calendar c = Calendar.getInstance();
			myYear = c.get(Calendar.YEAR);
			myMonth = c.get(Calendar.MONTH);
			myDay = c.get(Calendar.DAY_OF_MONTH);
			showDialog(ID_DATEPICKER);
		}
	};
		
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch(id){
			case ID_DATEPICKER:
				Toast.makeText(ProfileActivity.this, 
						"- onCreateDialog -", 
						Toast.LENGTH_LONG).show();
				return new DatePickerDialog(this,
						myDateSetListener,
						myYear, myMonth, myDay);
			default:
				return null;
		}
	}
	 @Override
	    protected void onPrepareDialog(int id, Dialog dialog) {
	        switch (id) {
	           
	            case ID_DATEPICKER:
	                ((DatePickerDialog) dialog).updateDate(myYear, myMonth, myDay);
	                break;
	        }
	    }    

	private DatePickerDialog.OnDateSetListener myDateSetListener
		= new DatePickerDialog.OnDateSetListener(){
			
			@Override
			public void onDateSet(android.widget.DatePicker  view, int year,
					int monthOfYear, int dayOfMonth) {
				// TODO Auto-generated method stub
				String date = "Year: " + String.valueOf(year) + "\n"
				+ "Month: " + String.valueOf(monthOfYear+1) + "\n"
				+ "Day: " + String.valueOf(dayOfMonth);
			Toast.makeText(ProfileActivity.this, date, 
					Toast.LENGTH_LONG).show();
				
			}
	};
}
